<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/4
 * Time: 20:08
 */

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Model\Apply;
use App\Model\BankCard;
use App\Model\ExceedShop;
use App\Model\Members;
use App\Model\MemberSign;
use App\Model\MembersLevel;
use App\Model\MicroMall;
use App\Model\Poundage;
use App\Model\RecordBonus;
use App\Model\RecordTransaction;
use App\Model\RecordTransferAccounts;
use App\Model\RecordWithdraw;
use App\Model\ShopMemberCart;
use App\Model\ShopOrder;
use App\Model\StraightShopp;
use Illuminate\Http\Request;
use App\Model\Address;
use App\Model\ShopGoodsImg;
use App\Model\ShopGoods;
use Illuminate\Support\Facades\DB;
use Session;

class MemberTestController extends Controller
{
    public function daili()
    {
        //会员表
        $UserModel = new Members();
        $users = $UserModel->where('uid' ,'>', 1705)->where(['straight_shop_isdown' => 2, 'isdown' => 1])->select('uid','super_level','phone','super_recommend')->get();
//        dump(count($users));exit;
        foreach ($users as $k=>$v) {
            $count = $UserModel->where(['referrer' => $v['uid']])->count();
            $v['super_recommend'] = $count;
//            $data['super_recommend'] = $count;
//            if($count < 10 ){
//                $data['super_level'] = 1;
//            }
//            $data['uid'] = $v['uid'];
//            $data['phone'] = $v['phone'];
//            dump($v);
//            dump($data);
            $a = $UserModel->where(['uid' => $v['uid']])->update($v['attributes']);
            dump($a);
        }
//        $uid[0] = 118;
//        $this->StraightShopIsdown($user,$uid,117);
    }
    /**
     * 修改直商城开关
     * @param array $data
     * @param array $uid
     * @param array $pid
     * @param int $type
     */
    public function StraightShopIsdown($data = array(),$uid =  array() , $pid = array(),$type = 2)
    {
        $pid = $pid;
        $nodesid = [];
        foreach ($data as $k=>$v){
            foreach ($uid as $key => $value){
                if($v['referrer'] == $value){
                    $pid[] = $v['uid'];
                    $nodesid[] = $v['uid'];
                }
            }
        }
        if(!empty($nodesid)){
            $this->StraightShopIsdown($data , $nodesid,$pid,$type);
        } else {
            dump(count($pid));
        }
    }
    //代理费
    public function daili1(Request $request)
    {
        exit();
        $money  = $request->get('money');
        $uid  = $request->get('uid');
        if(empty($money)){
            exit();
        }
        if(empty($uid)){
            exit();
        }
        //系统设置
        $SetPoundage = new Poundage();
        $Poundage = $SetPoundage->where(['id' => 1])->first()->toArray();
        //会员表
        $UserModel = new Members();
        //查询代理区域
        $userApply = new Apply();
        //省
//        $userProvince = $userApply->where(['type' => 0,'province' => $province,'status' => 1])->first();
//        if($userProvince){
            //计算金额
            $provinceMoney = RETAIN_TWO($money * (15 * 0.01));//市代理
            //扣除平台费和复投币
            $recommendMoney = CALCULATE($Poundage,$provinceMoney);
            $member = $UserModel->find_members(['uid' => $uid],['uid','credit1','credit2','credit3','credit4']);
            if ($member) {
                $member['credit2'] +=  $recommendMoney['money'];
                $member['credit3'] +=  $recommendMoney['redelivery'];
                //添加记录
                $this->addOrder($member['uid'] , $provinceMoney , 5 ,'市代理分润', $recommendMoney,0);
                //更新会员数据
                $rico = $UserModel->where(['uid' => $member['uid']])->update($member['attributes']);
                dump($rico);
            }

//        }
    }
    //扣除
    public function reduce()
    {
        exit();
        //系统设置
        $SetPoundage = new Poundage();
        $Poundage = $SetPoundage->where(['id' => 1])->first()->toArray();
        $levelingMoney = CALCULATE($Poundage,90);
        //会员表
        $UserModel = new Members();
        $phone = [
            //见点奖
//            0=>13892283302, //陈士能
//            1=>13992289733,
//            2=>18049358093,
//            3=>18740625528,
//            4=>13772329832,
//            5=>15596066763,
//            6=>13484484013,
//            7=>18690456677,
//            8=>13109216098,
//            9=>13891259115,
//            0=>13891259115,//见点奖
//            0=>13891259115,//见点奖
//            0=>15947070382,//27
//            0=>18709120630,//45
//            0=>17691134495,//90
        ];
        $user = $UserModel->whereIn('phone'  ,$phone)->get();
        foreach ($user as $k => $v){
//            dump($v['credit2']);
//            dump($v['credit3']);
            $data['credit2'] =  $v['credit2']-$levelingMoney['money'];
            $data['credit3'] =  $v['credit3']-$levelingMoney['redelivery'];
//            dump($levelingMoney);
//            $UserModel->where(['uid' => $v['uid']])->update($data);

        }
    }
    public function addMember()
    {
        exit('退出!');
        $data['nodes'] = 1667;

        //系统设置
        $SetPoundage = new Poundage();
        $Poundage = $SetPoundage->where(['id' => 1])->first()->toArray();
        //直商城设置
        $StraightShopp = new StraightShopp();
        $Straigh = $StraightShopp->where(['id' => 1])->first()->toArray();
        //会员表
        $UserModel = new Members();
        //奖励添加
        $memberDatas = $UserModel->where('uid','<=',$data['nodes'])->orderBy('uid','desc')->select('uid','nodes','referrer','area','credit1','credit2','credit3','credit4','performance1','performance2','performance_a','performance_b','leveling')->get()->toArray();
        $account = $this->bump($data['nodes'],'A',3000,$Straigh,$Poundage,0,$memberDatas);
        //计算管理奖
        if(!empty($account)){
            foreach ($account as $k => $v) {
                //添加管理奖
                $this->ManageAward($v['referrer'],$v['levelingAward'],$v['Straigh'],$v['Poundage'], 0);
            }
        }
    }
    /**
     * 奖励结算
     * @param int $pid 上级id
     * @param $area 区域
     * @param int $money 金额
     * @param array $Straigh 直商城设置
     * @param array $Poundage 系统设置
     * @param int $company_id 公司id
     * @return array 产生对碰的会员id
     */
    public function bump($pid = null,$area , $money = 0 , $Straigh = array() , $Poundage = array(),$company_id = 0,$referrer = [])
    {
//        DB::beginTransaction(); //开启事务
        //会员表
        $UserModel = new Members();
        //管理奖id
        $managementId = [];
        $number = 1;
        //节点人
        $area = $area;
        $pids = $pid;
        foreach ($referrer as $k => $v){
            if($pids == $v['uid']){ //寻找上级id
                $pids = $v['nodes'];
                if($number >= $Straigh['touch_number']){
                    if($area == 'A'){ //如果是a区就取 performance_b 的业绩出来碰
                        if ($v['performance_b'] > 0) {
                            /*----------------计算 对碰奖---------------*/
                            $performanceMoney = ($v['performance_b'] > $money) ? $money : $v['performance_b'];
                            //计算对碰奖
                            $levelingAward = RETAIN_TWO($performanceMoney * ($Straigh['touch'] * 0.01));
                            $levelingMoney = CALCULATE($Poundage,$levelingAward);
                            //计算大区业绩
                            $regionMoney = ($v['performance_b'] > $money) ? $v['performance_b']- $money : $money - $v['performance_b'];
                            $v['credit2'] +=  $levelingMoney['money'];
                            $v['credit3'] +=  $levelingMoney['redelivery'];
                            $v['performance_b'] =  RETAIN_TWO($regionMoney);

//                            $v['performance_a'] -=  $money;
                            //添加记录
                            $this->addOrder($v['uid'] , $levelingAward , 5 ,'对碰奖', $levelingMoney,$company_id);
                            /*----------------计算 共赢点(管理奖)---------------*/
                            //添加管理奖
                            $managementData = [
                                'referrer' => $v['referrer'],
                                'levelingAward' => $levelingAward,
                                'Straigh' => $Straigh,
                                'Poundage' => $Poundage,
                            ];
                            $managementId [] = $managementData;
                        } else {
                            //如果没有碰直接加奖励
//                            $v['performance_a'] += $money;
                        }
                    } else { //如果是b区就取performance_a的业绩出来碰
                        if ($v['performance_a'] > 0) {
                            /*----------------计算 对碰奖---------------*/
                            //取出最小
                            $performanceMoney = ($v['performance_a'] > $money) ? $money : $v['performance_a'];
                            //计算对碰奖
                            $levelingAward = RETAIN_TWO($performanceMoney * ($Straigh['touch'] * 0.01));
                            $levelingMoney = CALCULATE($Poundage,$levelingAward);
                            //计算大区业绩
                            $regionMoney = ($v['performance_a'] > $money) ? $v['performance_a']- $money : $money - $v['performance_a'];
                            $v['credit2'] +=  $levelingMoney['money'];
                            $v['credit3'] +=  $levelingMoney['redelivery'];
                            $v['performance_a'] =  RETAIN_TWO($regionMoney);
//                            $v['performance_b'] -=  $money;
                            //添加记录
                            $this->addOrder($v['uid'] , $levelingAward , 5 ,'对碰奖', $levelingMoney,$company_id);
                            /*----------------计算 共赢点(管理奖)---------------*/
                            $managementData = [
                                'referrer' => $v['referrer'],
                                'levelingAward' => $levelingAward,
                                'Straigh' => $Straigh,
                                'Poundage' => $Poundage,
                            ];
                            $managementId [] = $managementData;
                        } else {
                            //如果没有碰直接加奖励
//                            $v['performance_b'] += $money;
                        }
                    }
                }
                $area = $v['area'];
                ++$number;
                //更新会员数据
                $UserModel->where(['uid' => $v['uid']])->update($v);
            }
        }
        return $managementId;
    }
    /**
     * 管理奖
     * @param int $pid 上级id
     * @param int $money 金额
     * @param array $Straigh 直商城设置
     * @param array $Poundage 系统设置
     * @param int $company_id 公司id
     * @param int $number
     */
    public function  ManageAward($pid = 0 , $money = 0 , $Straigh = array() , $Poundage = array(),$company_id = 0 ,$number = 1)
    {
        //会员表
        $UserModel = new Members();
        //节点人
        $referrer = $UserModel->where('uid','=',$pid)->select('uid','nodes','referrer','area','credit1','credit2','credit3','credit4')->first();
        if(!empty($referrer)){
            if($number == 1){
                //计算 共赢点(管理奖)
                $manageAward = RETAIN_TWO($money * ($Straigh['win_win1'] * 0.01));
                $manageMoney = CALCULATE($Poundage,$manageAward);
                $levelinData = [ //推荐人奖励添加 DATA
                    'credit2' => $referrer['credit2'] + $manageMoney['money'],
                    'credit3' => $referrer['credit3'] + $manageMoney['redelivery'],
                ];
                $UserModel->where(['uid' => $referrer['uid']])->update($levelinData);
                //添加管理奖 记录
                $this->addOrder($referrer['uid'] , $manageAward , 5 ,'共赢点', $manageMoney,$company_id);
            } elseif ($number == 3) {
                //计算 共赢点(管理奖)
                $manageAward = RETAIN_TWO($money * ($Straigh['win_win3'] * 0.01));
                $manageMoney = CALCULATE($Poundage,$manageAward);
                $levelinData = [ //推荐人奖励添加 DATA
                    'credit2' => $referrer['credit2'] + $manageMoney['money'],
                    'credit3' => $referrer['credit3'] + $manageMoney['redelivery'],
                ];
                $UserModel->where(['uid' => $referrer['uid']])->update($levelinData);
                //添加管理奖 记录
                $this->addOrder($referrer['uid'] , $manageAward , 5 ,'共赢点', $manageMoney,$company_id);
            } elseif ($number == 5) {
                //计算 共赢点(管理奖)
                $manageAward = RETAIN_TWO($money * ($Straigh['win_win5'] * 0.01));
                $manageMoney = CALCULATE($Poundage,$manageAward);
                $levelinData = [ //推荐人奖励添加 DATA
                    'credit2' => $referrer['credit2'] + $manageMoney['money'],
                    'credit3' => $referrer['credit3'] + $manageMoney['redelivery'],
                ];
                $UserModel->where(['uid' => $referrer['uid']])->update($levelinData);
                //添加管理奖 记录
                $this->addOrder($referrer['uid'] , $manageAward , 5 ,'共赢点', $manageMoney,$company_id);
            }
            ++$number;
            if($number <= 5){
                if($referrer['referrer']){
                    $this->ManageAward($referrer['referrer'], $money , $Straigh , $Poundage, $company_id, $number);
                }
            }
        }

    }
    /**
     * 添加交易记录
     * @param null $uid 会员id
     * @param null $money 交易总金额
     * @param null $type 交易类型 1 订单 2 转账 3 提现  4充值 5 奖励制度
     * @param string $explain 交易名称
     * @param array $manageMoney 奖励金额 复投金额 平台费金额
     * @param null $company_id 公司id
     */
    public function addOrder($uid = null , $money = null , $type = null ,$explain = '', $manageMoney = array(),$company_id = null)
    {
        //交易记录表
        $RecordTransaction = new RecordTransaction();
        //交易记录表详情表
        $RecordBonus = new RecordBonus();
        $levelinDatas = [ //交易记录
            'uid'       => $uid,
            'ordersn'   => ORDER_SN(),
            'money'     => $money,
            'type'      => $type,
            'time'      => time(),
            'explain'   => $explain,
            'company_id' => $company_id,
        ];
//        dump($levelinDatas);
        $TransactionId = $RecordTransaction->add_transaction($levelinDatas);
//        $TransactionId = 11;
        $bonusData = [//交易详情
            'uid' => $uid,
            'tid' => $TransactionId,
            'money' => $money,
            'remark' => $explain.'：'.$manageMoney['money'].'，复投费：'.$manageMoney['redelivery'].'，平台费：'. $manageMoney['platform_fee'],
            'time' => time(),
            'company_id' => $company_id,
        ];
//        dump($bonusData);
        $RecordBonus->add_bonus($bonusData);
    }
}